﻿using BCCommon;
using BCData.MySql.BusinessFlowInstance;
using BCData.MySql.BusinessFlowStepInstance;
using BCDto.MySql.BusinessFlowStepInstance;
using BCEntity.MySql.BusinessFlowStepInstance;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BCService.OA.BusinessFlowInstance.BusinessFlowStepInstance
{
    public class BusinessFlowStepInstanceService : IBusinessFlowStepInstanceService
    {
        private readonly IBusinessFlowStepInstanceData businessFlowStepInstanceData;
        private readonly IBusinessFlowInstanceData businessFlowInstanceData;
        public BusinessFlowStepInstanceService(IBusinessFlowStepInstanceData businessFlowStepInstanceData, IBusinessFlowInstanceData businessFlowInstanceData)
        {
            this.businessFlowStepInstanceData = businessFlowStepInstanceData;
            this.businessFlowInstanceData = businessFlowInstanceData;
        }

        public bool Exists(long businessFlowStepInstanceId)
        {
            return this.businessFlowStepInstanceData.Exists(businessFlowStepInstanceId).GetAwaiter().GetResult();
        }


        public IEnumerable<BusinessFlowStepInstanceDto> Add(List<BusinessFlowStepInstanceRequestDto> requestDtos)
        {
            if (!requestDtos.Any())
            {
                throw new ArgumentException("业务流步骤信息不应为空");
            }
            long businessFlowInstanceId = requestDtos[0].BusinessFlowInstanceId;
            foreach (var dto in requestDtos)
            {
                if (!this.businessFlowInstanceData.Exists(dto.BusinessFlowInstanceId).GetAwaiter().GetResult())
                {
                    throw new ArgumentException("业务流实例不存在");
                }
            }
            var entities = requestDtos.As<List<BusinessFlowStepInstanceEntity>>();

            if (!this.businessFlowStepInstanceData.Add(entities).GetAwaiter().GetResult())
            {
                throw new ArgumentException("添加业务流步骤实例失败");
            }
            return this.GetList(businessFlowInstanceId);
        }

        public BusinessFlowStepInstanceDto GetEntity(long businessFlowStepInstanceId)
        {
            return this.businessFlowStepInstanceData.GetEntity(businessFlowStepInstanceId).GetAwaiter().GetResult().As<BusinessFlowStepInstanceDto>();
        }

        public IEnumerable<BusinessFlowStepInstanceDto> GetList(long businessFlowInstanceId)
        {
            return this.businessFlowStepInstanceData.GetList(businessFlowInstanceId).GetAwaiter().GetResult().As<IEnumerable<BusinessFlowStepInstanceDto>>();
        }

        public PageableList<BusinessFlowStepInstanceDto> Query(long? businessFlowModelId, long? businessFlowInstanceId, long? pageBusinessConfigId, string businessFlowStepInstanceName, DateTime? start, DateTime? end, int pageIndex, int pageSize)
        {
            var (list, count) = this.businessFlowStepInstanceData.Query(businessFlowModelId, businessFlowInstanceId, pageBusinessConfigId, businessFlowStepInstanceName, start, end, pageIndex, pageSize).GetAwaiter().GetResult();

            return new PageableList<BusinessFlowStepInstanceDto>
            {
                Items = list.As<IEnumerable<BusinessFlowStepInstanceDto>>(),
                Count = count,
                PageIndex = pageIndex,
                PageSize = pageSize
            };
        }
    }
}